//
//  OrderCalendarView.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/3/31.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "OrderCalendarView.h"

@interface OrderCalendarView ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic ,strong) UIView *backView;
@property (nonatomic ,strong) UIPickerView *pickerView;
@property (nonatomic ,strong) NSMutableArray *myArray1; //年
@property (nonatomic ,strong) NSMutableArray *myArray;  //月
@property (nonatomic ,strong) NSString *yearsStr;
@property (nonatomic ,strong) NSString *monthStr;
@property (nonatomic ,strong) NSString *timeSelectedString;

@end

@implementation OrderCalendarView


-(NSMutableArray *)myArray1{
    if (!_myArray1) {
        self.myArray1 = [[NSMutableArray alloc]init];
        for (int i = 1; i<13; i++) {
            NSString *str = [NSString stringWithFormat:@"%d%@",i,@"月"];
            [self.myArray1 addObject:str];
        }
    }
    return _myArray1;
}
-(NSMutableArray *)myArray{
    if (!_myArray) {
        self.myArray = [[NSMutableArray alloc]init];
        for (int i = 2000; i<2100; i++) {
            NSString *str = [NSString stringWithFormat:@"%d%@",i,@"年"];
            [self.myArray addObject:str];
        }
    }
    return _myArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        [self setup];
        [self setDataup];
    }
    return self;
}

- (void)setDataup {
    
    //初始时间选择文字
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM月"];
    self.monthStr = [formatter stringFromDate:[NSDate date]];
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"yyyy年"];
    self.yearsStr = [formatter1 stringFromDate:[NSDate date]];
    self.timeSelectedString = [NSString stringWithFormat:@"%@%@",self.yearsStr,self.monthStr];
    //设置pickerview初始默认
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"MM"];
    NSString *str111 = [formatter2 stringFromDate:[NSDate date]];
    NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
    [formatter3 setDateFormat:@"yyyy"];
    NSString *str222 = [formatter3 stringFromDate:[NSDate date]];
    int a = [str111 intValue];
    int b = [str222 intValue];
    [self.pickerView selectRow:a-1 inComponent:1 animated:NO];
    [self.pickerView selectRow:b-2000 inComponent:0 animated:NO];

    
}

- (void)setup {
    
    // 背景
    UIView *backView = [[UIView alloc] initWithFrame:(CGRectMake((self.frame.size.width - 250)/2, (self.frame.size.width - 250)/2, 250, 250))];
    backView.backgroundColor = [UIColor whiteColor];
    backView.layer.masksToBounds = YES;
    backView.layer.cornerRadius = 30;
    [self addSubview:backView];
    self.backView = backView;
    
    // 当前年月
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:(CGRectMake(20, 10, 100, 20))];
    timeLabel.textColor = [UIColor blackColor];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月"];
    NSString *timeStr = [formatter stringFromDate:[NSDate date]];
    timeLabel.text = timeStr;
    [backView addSubview:timeLabel];
    
    // 顶部线
    UIView *lineOneView = [[UIView alloc] initWithFrame:(CGRectMake(0, 40, size_width, 1))];
    lineOneView.backgroundColor = [UIColor blackColor];
    [backView addSubview:lineOneView];
    
    // 选择时间
    UIPickerView *picker = [[UIPickerView alloc] initWithFrame:(CGRectMake(0, lineOneView.frame.size.height + lineOneView.frame.origin.y, backView.frame.size.width, 170))];
    picker.backgroundColor = [UIColor whiteColor];
    picker.dataSource = self;
    picker.delegate = self;
    [backView addSubview:picker];
    self.pickerView = picker;
    
    // 顶部线
    UIView *lineTwoView = [[UIView alloc] initWithFrame:(CGRectMake(0, backView.frame.size.height - 40, size_width, 1))];
    lineTwoView.backgroundColor = [UIColor blackColor];
    [backView addSubview:lineTwoView];

    // 完成按钮
    UIButton *completeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    completeBtn.frame = CGRectMake((backView.frame.size.width - 50)/2, backView.frame.size.height - 30, 50, 20);
    [completeBtn setTitle:@"完成" forState:(UIControlStateNormal)];
    [completeBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [completeBtn addTarget:self action:@selector(completeBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [backView addSubview:completeBtn];
    
}

#pragma mark - UIPickView的代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        return 100;
    }else{
        return 12;
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return  self.myArray[row];
    }else {
        return  self.myArray1[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (component == 0) {
        self.yearsStr = self.myArray[row];
    }else{
        self.monthStr = self.myArray1[row];
    }
//    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
//    [formatter2 setDateFormat:@"MM"];
//    NSString *str111 = [formatter2 stringFromDate:[NSDate date]];
//    NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
//    [formatter3 setDateFormat:@"yyyy"];
//    NSString *str222 = [formatter3 stringFromDate:[NSDate date]];
//    int a = [str111 intValue];
//    int b = [str222 intValue];
//    int c = [self.monthStr intValue];
//    int d = [self.yearsStr intValue];
//    if (d>b||(d==b&&c>a)) {
//        [self.pickerView selectRow:a-1 inComponent:1 animated:YES];
//        [self.pickerView selectRow:b-2000 inComponent:0 animated:YES];
//    }
    
    self.timeSelectedString = [NSString stringWithFormat:@"%@%@",self.yearsStr,self.monthStr];
    NSLog(@"%@",self.timeSelectedString);
    
//    if (component == 0) {
//        NSString *str1 = [_myArray objectAtIndex:row];
//        NSLog(@"年%@",str1);
//    }else{
//        NSString *str2 = [_myArray1 objectAtIndex:row];
//        NSLog(@"月%@",str2);
//    }
    
}

// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    return 100;

}
#pragma mark - 完成按钮
- (void)completeBtnClicked:(UIButton *)sender {
    
    self.selectTime(self.timeSelectedString);
    
    [self removeFromSuperview];

}

#pragma mark - 弹出 -
- (void)showCustomCalendarView {

    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

- (void)creatShowAnimation {
    
    self.backView.layer.position = self.center;
    self.backView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];


}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    }];
}

@end
